package main

import (
	"fmt"
	"math/rand"
	"sort"
)

//对结构体排序
type Hero struct {
	Name string
	Age  int
}

type HeroSlice []Hero

func (h HeroSlice) Len() int {
	return len(h)
}

//less就是决定使用什么标准进行排序
//按年龄age升序排序
func (h HeroSlice) Less(i, j int) bool {
	return h[i].Age > h[j].Age
}

func (h HeroSlice) Swap(i, j int) {
	/*temp := h[i]
	h[i] = h[j]
	h[j] = temp*/
	h[i], h[j] = h[j], h[i]
}

//时间Interface接口
func main() {
	var heros HeroSlice
	for i := 0; i < 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("英雄%d", rand.Intn(100)),
			Age:  rand.Intn(100),
		}
		heros = append(heros, hero)
	}
	sort.Sort(heros)
	for _, hero := range heros {
		fmt.Println(hero)
	}

}
